using System.Collections;
using System.Collections.Generic;

namespace Phoenix.Xna.Framework.Collections
{
    public class MultiMap<K, V> : IEnumerable<KeyValuePair<K, List<V>>>
    {
        private Dictionary<K, List<V>> _innerDictionary;

        public List<V> Values
        {
            get
            {
                List<V> list = new List<V>();

                foreach (List<V> sub in _innerDictionary.Values)
                {
                    list.AddRange(sub);
                }

                return list;
            }
        }
        public V this[K key, int index]
        {
            get { return _innerDictionary[key][index]; }
        }

        public MultiMap()
        {
            _innerDictionary = new Dictionary<K, List<V>>();
        }

        public bool ContainsKey(K key)
        {
            return _innerDictionary.ContainsKey(key);
        }

        public void Add(K key, V item)
        {
            List<V> list;

            if (_innerDictionary.ContainsKey(key))
            {
                list = _innerDictionary[key];
            }
            else
            {
                list = new List<V>();
                _innerDictionary[key] = list;
            }

            list.Add(item);
        }

        public void Clear()
        {
            _innerDictionary.Clear();
        }

        public List<V> GetValues(K key)
        {
            return _innerDictionary[key];
        }

        #region IEnumerable<KeyValuePair<K,List<V>>> Members

        public IEnumerator<KeyValuePair<K, List<V>>> GetEnumerator()
        {
            return _innerDictionary.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return _innerDictionary.GetEnumerator();
        }

        #endregion
    }
}
